package string.homework;

/**
 * StringBuilder类:修改字符串的常见方式：增、删、改、插
 */

/**
 * 不使用StringBuilder：性能低
 */
public class StringAndStringBuilderDemo {
    public static void main(String[] args) {
        String str = "abc";
        for (int i = 0; i < 10000000; i++) {
            str = str + "abc";
        }
        System.out.println("完成");
    }
}

/**
 * StringBuilder：性能高
 */
class StringAndStringBuilder {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("abc");
        for (int i = 0; i < 10000000; i++) {
            builder.append("abc");
        }
        System.out.println("完成");
    }
}

/**
 * 增、删、改、插
 */
class StringBuilderDemo {
    public static void main(String[] args) {
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder("abc123456");

        String str3 = "aaa123";
        StringBuilder builder3 = new StringBuilder(str3);
        String str2 = builder3.toString();//将builder3转换为String

        String str = "热爱学习Java";
        StringBuilder builder = new StringBuilder(str);

        builder = builder.append(",也喜欢学习Python");
        System.out.println(builder);//热爱学习Java,也喜欢学习Python
        builder = builder.replace(14, 20, "C++");
        System.out.println(builder);//热爱学习Java,也喜欢学习C++
        builder = builder.delete(0, 9);
        System.out.println(builder);//也喜欢学习C++
        builder = builder.insert(0, "热爱学习Java,");
        System.out.println(builder);//热爱学习Java,也喜欢学习C++
        builder = builder.reverse();
        System.out.println(builder);//++C习学欢喜也,avaJ习学爱热
        builder = builder.reverse();
        System.out.println(builder);//热爱学习Java,也喜欢学习C++
    }
}